import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def main():
    driver = webdriver.Chrome()
    driver.get('https://www.xiaohongshu.com/explore')
    driver.maximize_window()
    driver.implicitly_wait(30)
    time.sleep(2)
    button_able = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div[3]/div[2]/form/label[1]/input')
    print(button_able)
    button_able.send_keys('17825230904')
    time.sleep(1)
    button_able1 = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div[3]/div[2]/form/label[2]/span')
    button_able1.click()
    time.sleep(20)
    button_able3 = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div[3]/div[3]/span/div')
    button_able3.click()
    button_able2 = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/div[3]/div[2]/form/button')
    button_able2.click()
    time.sleep(2)
    button_able4 = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[1]/header/div[1]/input')
    button_able4.send_keys('银饰饰')
    time.sleep(1)
    # 搜索用户
    button_able5 = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[1]/header/div[1]/div/div[2]')
    button_able5.click()
    time.sleep(2)
    #点击用户
    button_able6 = driver.find_element(By.XPATH, '/html/body/div[2]/div[1]/div[2]/div[2]/div/div/div[2]/div[1]/a/div')
    button_able6.click()
    time.sleep(2)

    #跳到第二页
    # 等待新窗口打开，获取所有窗口句柄
    WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) > 1)
    window_handles = driver.window_handles
    # 切换到第二个窗口（假设是新打开的页面所在窗口）
    driver.switch_to.window(window_handles[1])

    # 循环视频
    i=1
    j=1
    while True:
        # 点击视频
        type_value=f"/html/body/div[2]/div[1]/div[2]/div[2]/div/div[3]/div/div[1]/div[1]/section[{i}]"
        button_able7 = driver.find_element(By.XPATH,type_value)
        button_able7.click()
        time.sleep(5)
        # 关闭窗口
        button_able8 = driver.find_element(By.XPATH, '/html/body/div[6]/div[2]/div')
        button_able8.click()
        time.sleep(1)
        i=i+1
        if i>10:
            i=1
            j=j+1
            if j>20:
                break

    time.sleep(60)


if __name__ == '__main__':
    main()